//
//  RHMeDeviceInfoCell.m
//  RuiHeApp
//
//  Created by Yan Chen on 2025/1/7.
//

#import "RHMeDeviceInfoCell.h"
#import "RHDeviceDetailModel.h"

@interface RHMeDeviceInfoCell()

@property (nonatomic ,strong) QMUILabel *deviceIdLab;
//部件数量
@property (nonatomic ,strong) QMUILabel *componentCountLab;
//额定容量
@property (nonatomic ,strong) QMUILabel *capacityLab;
//额定功率
@property (nonatomic ,strong) QMUILabel *powerLab;

//今日充电
@property (nonatomic ,strong) QMUILabel *todayChgLab;
//累计充电
@property (nonatomic ,strong) QMUILabel *totalChgLab;
//今日放电
@property (nonatomic ,strong) QMUILabel *todayDchgLab;
//累计放电
@property (nonatomic ,strong) QMUILabel *totalDchgLab;


//告警次数
@property (nonatomic ,strong) QMUILabel *alarmCountLab;
//故障次数
@property (nonatomic ,strong) QMUILabel *faultCountLab;

@end
@implementation RHMeDeviceInfoCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self setupUI];
    }
    return self;
}

-(void)setInfoModel:(RHDeviceDetailModel *)infoModel
{
    _infoModel = infoModel;
    self.deviceIdLab.text = infoModel.deviceId;
    //部件数量
    NSString *componentCount = [NSDecimalNumber decimalStringWithDoubleString:infoModel.componentNumber nullStr:@"--"];
    [self.componentCountLab setText:componentCount];
    
    //额定容量
    NSString *capacityValue = [NSDecimalNumber decimalStringWithDoubleString:infoModel.ratedCapacity nullStr:@"--"];
    [self.capacityLab setText:[NSString stringWithFormat:@"%@ kWh",capacityValue]];
    self.capacityLab.attributedText = [self attrStringWithContent:self.capacityLab.text attrKey:@"kWh"];
    
    //额定功率
    NSString *powerValue = [NSDecimalNumber decimalStringWithDoubleString:infoModel.ratedPower nullStr:@"--"];
    [self.powerLab setText:[NSString stringWithFormat:@"%@ kW",powerValue]];
    self.powerLab.attributedText = [self attrStringWithContent:self.powerLab.text attrKey:@"kW"];
    
    //今日充电
    NSString *todayChgValue = [NSDecimalNumber decimalStringWithDoubleString:infoModel.todayRecharge nullStr:@"--"];
    [self.todayChgLab setText:[NSString stringWithFormat:@"%@ kWh",todayChgValue]];
    self.todayChgLab.attributedText = [self attrStringWithContent:self.todayChgLab.text attrKey:@"kWh"];
    
    //累计充电
    NSString *totalChgValue = [NSDecimalNumber decimalStringWithDoubleString:infoModel.accumulateRecharge nullStr:@"--"];
    [self.totalChgLab setText:[NSString stringWithFormat:@"%@ kWh",totalChgValue]];
    self.totalChgLab.attributedText = [self attrStringWithContent:self.totalChgLab.text attrKey:@"kWh"];
    
    //今日放电
    NSString *todayDchgValue = [NSDecimalNumber decimalStringWithDoubleString:infoModel.todayDischarge nullStr:@"--"];
    [self.todayDchgLab setText:[NSString stringWithFormat:@"%@ kWh",todayDchgValue]];
    self.todayDchgLab.attributedText = [self attrStringWithContent:self.todayDchgLab.text attrKey:@"kWh"];
    
    //累计放电
    NSString *totalDchgValue = [NSDecimalNumber decimalStringWithDoubleString:infoModel.accumulateDischarge nullStr:@"--"];
    [self.totalDchgLab setText:[NSString stringWithFormat:@"%@ kWh",totalDchgValue]];
    self.totalDchgLab.attributedText = [self attrStringWithContent:self.totalDchgLab.text attrKey:@"kWh"];
    
    //告警次数
    NSString *alarmValue = [NSDecimalNumber decimalStringWithDoubleString:infoModel.alarmNumber nullStr:@"--"];
    [self.alarmCountLab setText:alarmValue];
    
    //故障次数
    NSString *faultValue = [NSDecimalNumber decimalStringWithDoubleString:infoModel.faultNumber nullStr:@"--"];
    [self.faultCountLab setText:faultValue];
   
}

- (void)setupUI{
    kWeakSelf;
    [self setBackgroundColor:UIColor.clearColor];
    [self.contentView setBackgroundColor:CFTabViewBgColor];
    
    UIView *bgView = [[UIView alloc] init];
    [bgView setBackgroundColor:CFTabViewBgColor];
    [self.contentView addSubview:bgView];
    
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0,0, 0, 0));
    }];

   //设备信息
    NSArray *infoTitles = @[@"部件数".rh_localized,@"额定容量".rh_localized,@"额定功率".rh_localized];
    UIView *infoView = [self infoViewWithTitle:@"设备信息".rh_localized icon:@"device_info" subTitles:infoTitles];
    [bgView addSubview:infoView];
    [infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.mas_offset(15);
    }];
    self.componentCountLab   = [infoView viewWithTag:100];
    self.capacityLab = [infoView viewWithTag:101];
    self.powerLab  = [infoView viewWithTag:102];
  

    //充放统计
    NSArray *chgTitles = @[@"今日充电".rh_localized,@"累计充电".rh_localized,@"今日放电".rh_localized,@"累计放电".rh_localized];
    UIView *totalView = [self itemWithTypeTitle:@"充放统计".rh_localized icon:@"plant_info_chg"  subTitles:chgTitles];
    [bgView addSubview:totalView];
    [totalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(infoView);
        make.right.equalTo(infoView);
        make.top.equalTo(infoView.mas_bottom).offset(15);
    }];
    self.todayChgLab  = [totalView viewWithTag:100];
    self.totalChgLab  = [totalView viewWithTag:101];
    self.todayDchgLab = [totalView viewWithTag:102];
    self.totalDchgLab = [totalView viewWithTag:103];
    
    
    //告警次数
    UIView *alarmView = [self alarmViewWithTypeTitle:@"告警次数".rh_localized icon:@"device_info_alarm"];
    [bgView addSubview:alarmView];
    [alarmView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(totalView.mas_bottom).offset(10);
        make.width.offset((kUI_WINDOW_W-40)/2);
        make.height.offset(70);
        make.bottom.mas_offset(-10);
    }];
    self.alarmCountLab = [alarmView viewWithTag:100];
    
    //故障次数
    UIView *faultView = [self alarmViewWithTypeTitle:@"故障次数".rh_localized icon:@"device_info_fault"];
    [bgView addSubview:faultView];
    [faultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(alarmView.mas_right).offset(10);
        make.top.equalTo(alarmView);
        make.width.height.equalTo(alarmView);
    }];
    self.faultCountLab = [faultView viewWithTag:100];
}


//设备信息
-(UIView*)infoViewWithTitle:(NSString*)title icon:(NSString*)icon  subTitles:(NSArray*)titles
{
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.qmui_borderColor = kBorderColor;
    bgView.qmui_borderWidth = 1;
    bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionRight|QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom;
  
    
    //左上角图标
    UIImageView *iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:icon]];
    [bgView addSubview:iconImg];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    //左上角标题
    QMUILabel *titleLab = [QMUILabel labelWithText:@"--" font:kBOLDSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#3F4443")];
    [bgView addSubview:titleLab];
    titleLab.text = title;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(10);
        make.centerY.equalTo(iconImg);
    }];
    
    //设备ID
    QMUILabel *deviceIdTitleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    deviceIdTitleLab.text = @"设备ID:".rh_localized;
    [deviceIdTitleLab sizeToFit];
    [bgView addSubview:deviceIdTitleLab];
    [deviceIdTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(iconImg.mas_bottom).offset(15);
        make.width.offset(deviceIdTitleLab.width+1);
    }];
    
    QMUILabel *deviceIdContentLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    deviceIdContentLab.text = @"--";
    [deviceIdContentLab sizeToFit];
    [bgView addSubview:deviceIdContentLab];
    self.deviceIdLab = deviceIdContentLab;
    [deviceIdContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(deviceIdTitleLab.mas_right);
        make.right.mas_offset(-10);
        make.top.equalTo(deviceIdTitleLab);
    }];
    
    
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(deviceIdContentLab.mas_bottom).offset(5);
        make.bottom.mas_offset(-10);
    }];
    
    
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 30-20-10;
    CGFloat itemWidth = bgWidth / 3;
    //左边第一个
    __block UIView *lastView;
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = kView;
        itemView.backgroundColor = kCOLOR_HEX(@"#F4F6F9");
        itemView.layer.cornerRadius = 8;
        itemView.layer.masksToBounds = YES;
        [itemBgView addSubview:itemView];
        QMUILabel *titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(13) textColor:kCOLOR_HEX(@"#8B8D92") textAligment:NSTextAlignmentLeft];
        titleLab.text = title;
        titleLab.numberOfLines = 0;
        [itemView addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.top.mas_offset(0);
        }];
        
        QMUILabel *contentLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
        contentLab.text = @"--";
        contentLab.numberOfLines = 2;
        contentLab.tag = 100+idx;
        [itemView addSubview:contentLab];
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab);
            make.right.mas_offset(-5);
            make.top.equalTo(titleLab.mas_bottom).offset(3);
            make.height.offset(18);
            make.bottom.mas_offset(-8);
        }];
        
        if (lastView) {
            if (idx%3==0) {
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(10);
                    make.size.equalTo(lastView);
                    make.top.equalTo(lastView.mas_bottom);
                }];
               
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right).offset(5);
                    make.size.top.equalTo(lastView);
                }];
             
            }
            
        }else{
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_offset(10);
                make.width.offset(itemWidth);
                make.height.offset(65);
            }];
        }
        
        
        
        lastView = itemView;
    }];
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    
    return bgView;
}

//充放统计

-(UIView*)itemWithTypeTitle:(NSString*)title icon:(NSString*)icon  subTitles:(NSArray*)titles
{
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.qmui_borderColor = kBorderColor;
    bgView.qmui_borderWidth = 1;
    bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionRight|QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom;
  
    
    //左上角图标
    UIImageView *iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:icon]];
    [bgView addSubview:iconImg];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(25);
        make.left.top.mas_offset(15);
    }];
    //左上角标题
    QMUILabel *titleLab = [QMUILabel labelWithText:@"--" font:kBOLDSYSTEMFONT(16) textColor:kCOLOR_HEX(@"#3F4443")];
    [bgView addSubview:titleLab];
    titleLab.text = title;
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(10);
        make.centerY.equalTo(iconImg);
    }];
    
    UIView *itemBgView = kView;
    [bgView addSubview:itemBgView];
    [itemBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(iconImg.mas_bottom).offset(5);
        make.bottom.mas_offset(-5);
    }];
    
    
    CGFloat bgWidth = MIN(kUI_WINDOW_W, kUI_WINDOW_H) - 30;
    CGFloat itemWidth = bgWidth / 2;
    //左边第一个
    __block UIView *lastView;
    [titles enumerateObjectsUsingBlock:^(NSString*  _Nonnull title, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *itemView = kView;
        [itemBgView addSubview:itemView];
        QMUILabel *titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
        titleLab.text = title;
        titleLab.numberOfLines = 0;
        [itemView addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.bottom.equalTo(itemView.mas_centerY).offset(-5);
        }];
        
        QMUILabel *contentLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
        contentLab.text = @"--";
        contentLab.numberOfLines = 2;
        contentLab.tag = 100+idx;
        [itemView addSubview:contentLab];
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLab);
            make.right.mas_offset(-5);
            make.top.equalTo(itemView.mas_centerY).offset(5);
        }];
        
        if (lastView) {
            if (idx%2==0) {
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(0);
                    make.size.equalTo(lastView);
                    make.top.equalTo(lastView.mas_bottom);
                }];
                //横线
                UIView *line_v = kView;
                line_v.backgroundColor = kCOLOR_HEX(@"#E3E6ED");
                [itemBgView addSubview:line_v];
                [line_v mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.mas_offset(20);
                    make.right.mas_offset(-20);
                    make.height.offset(1);
                    make.top.equalTo(lastView.mas_bottom).offset(-1);
                }];
            }else{
                [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(lastView.mas_right);
                    make.size.top.equalTo(lastView);
                }];
                //竖线
                UIView *line_h = kView;
                line_h.backgroundColor = kCOLOR_HEX(@"#E3E6ED");
                [itemBgView addSubview:line_h];
                [line_h mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(lastView).offset(20);
                    make.bottom.equalTo(lastView).offset(-20);
                    make.width.offset(1);
                    make.centerX.mas_offset(0);
                }];
            }
            
        }else{
            [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.mas_offset(0);
                make.width.offset(itemWidth);
                make.height.offset(76);
            }];
        }
        
        
        
        lastView = itemView;
    }];
    [lastView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(0);
    }];
    
    return bgView;
}

-(UIView*)alarmViewWithTypeTitle:(NSString*)title icon:(NSString*)icon
{
    UIView *bgView = kView;
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 8;
    bgView.layer.masksToBounds = YES;
    bgView.qmui_borderColor = kBorderColor;
    bgView.qmui_borderWidth = 1;
    bgView.qmui_borderPosition = QMUIViewBorderPositionLeft|QMUIViewBorderPositionRight|QMUIViewBorderPositionTop|QMUIViewBorderPositionBottom;
    
    //左上角图标
    UIImageView *iconImg = [[UIImageView alloc]initWithImage:[UIImage imageNamed:icon]];
    [bgView addSubview:iconImg];
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(30);
        make.left.mas_offset(15);
        make.centerY.mas_offset(0);
    }];
    
    QMUILabel *titleLab = [QMUILabel labelWithFont:kSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    titleLab.text = title;
    titleLab.numberOfLines = 0;
    [bgView addSubview:titleLab];
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImg.mas_right).offset(15);
        make.right.mas_offset(-15);
        make.centerY.equalTo(iconImg.mas_top);
    }];
    
    QMUILabel *contentLab = [QMUILabel labelWithFont:kBOLDSYSTEMFONT(14) textColor:kCOLOR_HEX(@"#303133") textAligment:NSTextAlignmentLeft];
    contentLab.text = @"--";
    contentLab.numberOfLines = 2;
    contentLab.tag = 100;
    [bgView addSubview:contentLab];
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLab);
        make.right.mas_offset(-5);
        make.top.equalTo(titleLab.mas_bottom).offset(10);
    }];
    return bgView;
}


-(NSAttributedString*)attrStringWithContent:(NSString *)content  attrKey:(NSString*)key
{
    NSDictionary *attiDic = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:10],NSForegroundColorAttributeName:[UIColor colorWithHexString:@"3F4443" alpha:0.6]};
    
    NSMutableAttributedString *attri_content = [[NSMutableAttributedString alloc] initWithString:content];
    [attri_content setAttributes:attiDic range:[content rangeOfString:key]];
    return attri_content;
}

@end
